package com.lxw.oa.controller.common;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import com.lxw.oa.controller.io.BaseTo;
import com.lxw.oa.entity.User;
import com.lxw.oa.service.StuSurveyService;
import com.lxw.oa.util.LogAopIn;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;


/**
 * 辅导员Controller
 * @author Administrator
 *
 */
@Controller
@EnableTransactionManagement
@RequestMapping("/stu/Survey")
public class StuMydController{

	@Autowired
	private StuSurveyService stuSurveyService;
	/**
	 * 学生反馈页面
	 * @return 返回页面
	 */
	@LogAopIn(moduleName="学生调查-学生满意度调查",option="页面初始化")
	@GetMapping("/toXsfk")
	public String Log_toXsfk(ModelMap modelMap){

		List<Map<String,String>> list = stuSurveyService.getClubs();
		//体育
		List<Map<String,String>> tyList =stuSurveyService.getTYList();
		//文学
		List<Map<String,String>> gwList =stuSurveyService.getWXList();
		//实践
		List<Map<String,String>> sjList =stuSurveyService.getSJList();
		modelMap.put("clubList", list);
		modelMap.put("tyList", tyList);
		modelMap.put("gwList", gwList);
		modelMap.put("sjList", sjList);
		return "pages/stuSurvey/StuMyd";
	}


	@LogAopIn(moduleName="学生调查-完成情况",option="页面初始化")
	@GetMapping("/toWcqk")
	public String Log_toWcqk(HttpServletRequest request, Model model){
		List<String> list=stuSurveyService.getXymcAll();
		model.addAttribute("xymc", list);
		return "pages/stuSurvey/Stu_Xsfk_Wcqk";
	}

	@LogAopIn(moduleName="学生调查-院级领导看板",option="页面初始化")
	@GetMapping("/toYjld")
	public String Log_toYjld(HttpServletRequest request, Model model){
		return "pages/stuSurvey/Stu_Xsfk_Yjld";
	}

	@GetMapping("/toYjldXy")
	public String toYjldXy(HttpServletRequest request, Model model){
		return "pages/stuSurvey/Stu_Xsfk_YjldXy";
	}

	@GetMapping("/toYjldZy")
	public String toYjldZy(HttpServletRequest request, Model model){
		List<String> list=stuSurveyService.getXymcAll();
		model.addAttribute("xymc", list);
		return "pages/stuSurvey/Stu_Xsfk_YjldZy";
	}

	@GetMapping("/toYjldReason")
	public String toYjldReason(Model model,String name){
		if("".equals(name)||name==null){
			name="";
		}
		List<String> list=stuSurveyService.getzlmcAll();
		model.addAttribute("zlmc", list);
		List<Map<String,String>> list1=stuSurveyService.getbadReason("%"+name+"%");
		model.addAttribute("badReason", list1);
		model.addAttribute("nameValue", name);
		return "pages/stuSurvey/Stu_Xsfk_YjldReason";
	}



	@PostMapping("/toSaveXsfk")
	@ResponseBody
	public BaseTo toSaveXsfk(HttpServletRequest request){
		//获取传输过来的所有key-value
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		String loginName = user.getName();
		Map<String, String> message = new HashMap<String, String>();

		//查询当前反馈表中是否包含当前学号
		List ifSub = stuSurveyService.ifSubmit(loginName);
		if(ifSub.size()>0){
			//已进行过投票
			message.put("is_fk", "1");
			return new BaseTo(message);
		}
		//查询当前反馈表中是否存在当前登录的用户的
		Map map=request.getParameterMap();
	    Set keSet=map.entrySet();
	    List<Map<String,String>> list = new ArrayList<Map<String,String>>();
	    for(Iterator itr = keSet.iterator(); itr.hasNext();){
			Map<String,String> info = new HashMap<String,String>();
	        Map.Entry me=(Map.Entry)itr.next();
	        String key=me.getKey().toString();
	        if(key.length()>4){
	        	//排斥单选以外的较差原因以及参加志愿者次数和社团选择
	        	if("stuCount".equals(key)){
	        		//保存参与志愿者次数
	        		String stuCount = ((String[])map.get(key))[0];
					stuSurveyService.addStuCount(loginName,stuCount);
	        	}
	        	if("checkClub".equals(key)){
	        		//保存社团多选
	        		List<Map<String,String>> clubChose = new ArrayList<Map<String,String>>();
	        		String clubs = ((String[])map.get(key))[0];
	        		if(!"".equals(clubs)&&null!=clubs){
	        			String[] split = clubs.split(",");
	        			for (String club : split) {
							Map<String, String> clubsChose = new HashMap<String, String>();
							clubsChose.put("Xh",loginName);
							clubsChose.put("ClubID",club);
							clubsChose.put("Reason","");
							if("other".equals(club)){
								String reason = ((String[])map.get("other_reason"))[0];
								clubsChose.put("Reason",reason);
							}
							clubChose.add(clubsChose);
						}
	        		}
					stuSurveyService.addClubs(clubChose);
	        	}

	        	continue;
	        }
	        Object ov=me.getValue();
	        String[] value=new String[1];
	        if(ov instanceof String[]){
	            value=(String[])ov;
	        }else{
	            value[0]=ov.toString();
	        }
	        info.put("Xh",loginName);
			info.put("Chose_Id",value[0]);
			info.put("Type_Id",key);
			info.put("Reason","");
	        //若选择为4，则获取原因
	        if("4".equals(value[0])){
	        	String reason = ((String[])map.get(key+"_reason"))[0];
				info.put("Reason",reason);
	        }
	        //添加单选按钮的keyValue
	        list.add(info);
	      }
	    //保存数据（单选）
		stuSurveyService.addList(list);
	    //到这反馈成功，修改用户状态，不能重复提交
		return new BaseTo(message);
	}

}
